//
//  ORealAuthResultViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/7/27.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "ORealAuthResultViewController.h"
#import <QuickLook/QuickLook.h>
#import "KJSRequest.h"

@interface ORealAuthResultViewController ()<QLPreviewControllerDataSource>

@property (nonatomic,strong) IBOutlet UILabel *oCompanyNameLab;
@property (nonatomic,strong) IBOutlet UILabel *oUanLab;
@property (nonatomic,strong) IBOutlet UILabel *oRepresentNameLab;
@property (nonatomic,strong) IBOutlet UILabel *oRepresentPhoneLab;
@property (nonatomic,strong) IBOutlet UILabel *oRepresentIdNumLab;
@property (nonatomic,strong) IBOutlet UILabel *oRepresentIdNumUrlLab;
@property (nonatomic,strong) IBOutlet UILabel *oCompanyBusinessLicenseUrlLab;
@property (nonatomic,strong) IBOutlet UILabel *oBankPermitLicenseUrlLab;
@property (nonatomic,strong) IBOutlet UILabel *auditingOpinionLab;

@property (nonatomic,strong) NSString *requestUrl;
@property (nonatomic,strong) NSString *saveUrl;
@property (nonatomic,strong) NSString *fileName;
@property (nonatomic,strong) UINavigationController *qlNavigationController;
@property (nonatomic,strong) QLPreviewController *qLPreviewController;

@end

@implementation ORealAuthResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.qLPreviewController = [[QLPreviewController alloc]initWithNibName:nil bundle:nil];
    self.qLPreviewController.navigationController.navigationBarHidden = YES;
    self.qLPreviewController.dataSource = self;
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(closeQuickLookAction:)];
    self.qLPreviewController.navigationItem.leftBarButtonItem = backButton;
    
    self.saveUrl = [[NSString alloc] init];
    self.requestUrl = [[NSString alloc] init];
    self.fileName = [[NSString alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%@",self.realAuthInfo);
    self.oCompanyNameLab.text = self.realAuthInfo.oCompanyName;
    self.oUanLab.text = self.realAuthInfo.oUan;
    self.oRepresentNameLab.text = self.realAuthInfo.oRepresentName;
    self.oRepresentIdNumLab.text = self.realAuthInfo.oRepresentIdNum;
    self.oRepresentPhoneLab.text = self.realAuthInfo.oRepresentPhone;
    if (self.realAuthInfo.oRepresentIdNumUrl){
        NSString *idNumImageType = [self.realAuthInfo.oRepresentIdNumUrl componentsSeparatedByString:@"."].lastObject;
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"身份证.%@",idNumImageType]];
        NSRange contentRange = {0,[content length]};
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
        self.oRepresentIdNumUrlLab.attributedText = content;
        self.oRepresentIdNumUrlLab.tag = 1;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
        [self.oRepresentIdNumUrlLab addGestureRecognizer:tap];
        self.oRepresentIdNumUrlLab.userInteractionEnabled = YES;
        
    }
    if (self.realAuthInfo.oCompanyBusinessLicenseUrl){
        NSString *BusinessImageType = [self.realAuthInfo.oCompanyBusinessLicenseUrl componentsSeparatedByString:@"."].lastObject;
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"营业执照图片.%@",BusinessImageType]];
        NSRange contentRange = {0,[content length]};
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
        self.oCompanyBusinessLicenseUrlLab.attributedText = content;
        self.oCompanyBusinessLicenseUrlLab.tag = 2;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
        [self.oCompanyBusinessLicenseUrlLab addGestureRecognizer:tap];
        self.oCompanyBusinessLicenseUrlLab.userInteractionEnabled = YES;
    }
    if (self.realAuthInfo.oBankPermitLicenseUrl){
        NSString *permitImageType = [self.realAuthInfo.oBankPermitLicenseUrl componentsSeparatedByString:@"."].lastObject;
        NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"开户许可证.%@",permitImageType]];
        NSRange contentRange = {0,[content length]};
        [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
        self.oBankPermitLicenseUrlLab.attributedText = content;
        self.oBankPermitLicenseUrlLab.tag = 3;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
        [self.oBankPermitLicenseUrlLab addGestureRecognizer:tap];
        self.oBankPermitLicenseUrlLab.userInteractionEnabled = YES;
    }
    
    if ([self.realAuthInfo.auditingOpinion isEqualToString:@"0"]) {
        self.auditingOpinionLab.text = @"您的实名认证信息审核不通过！";
    }else{
        self.auditingOpinionLab.text = @"您的实名认证信息审核已通过！";
    }
}
#pragma mark - custom method
-(void)showImage:(id)sender
{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    UIView *view = tap.view;
    NSLog(@"tag= %ld",view.tag);
    switch (view.tag) {
        case 1:
            self.requestUrl = ConnectUrl(self.realAuthInfo.oRepresentIdNumUrl);
            self.fileName = @"身份证.jpg";
            break;
        case 2:
            self.requestUrl = ConnectUrl(self.realAuthInfo.oCompanyBusinessLicenseUrl);
            self.fileName = @"公司营业执照.jpg";
            break;
        case 3:
            self.requestUrl = ConnectUrl(self.realAuthInfo.oBankPermitLicenseUrl);
            self.fileName = @"开户许可证.jpg";
            break;
        default:
            break;
    }
    NSLog(@"url=%@,filename=%@",self.requestUrl,self.fileName);
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.requestUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentsDirectory=[paths objectAtIndex:0];
        NSString *savedPath=[documentsDirectory stringByAppendingPathComponent:self.fileName];
        [data writeToFile:savedPath atomically:YES];
        self.saveUrl = savedPath;
        NSLog(@"self.saveUrl = %@",self.saveUrl);
        [self.qLPreviewController setCurrentPreviewItemIndex:0];
        [self.qLPreviewController reloadData];
        [self presentViewController:self.qlNavigationController animated:YES completion:nil];
    }];
    self.qlNavigationController = [[UINavigationController alloc]initWithRootViewController:self.qLPreviewController];
}
- (IBAction)closeQuickLookAction:(id)sender {
    NSLog(@"这里呗调用了");
    [_qlNavigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return 1;
}
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    return [NSURL fileURLWithPath:self.saveUrl];
}

@end
